package com.mall.db.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.db.mapper.SmsCouponProductRelationMapper;
import com.mall.pojo.SmsCouponProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponProductRelationApiImpl implements SmsCouponProductRelationApi{

    @Autowired
    private SmsCouponProductRelationMapper couponProductRelationMapper;

    /**
     * 添加商品与优惠券关系
     * @param productRelationList
     */
    @Override
    public void addIntoProductRelation(List<SmsCouponProductRelation> productRelationList) {
        for (SmsCouponProductRelation relation : productRelationList) {
            couponProductRelationMapper.insert(relation);
        }
    }

    /**
     * 根据优惠券id删除商品与优惠券关系
     * @param id
     */
    @Override
    public void deleteByCouponId(Integer id) {
        LambdaQueryWrapper<SmsCouponProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCouponProductRelation::getCouponId,id);
        couponProductRelationMapper.delete(wrapper);
    }

    /**
     * 根据优惠券id查询商品与优惠券关系
     * @param id
     * @return
     */
    @Override
    public List<SmsCouponProductRelation> findByCouponId(Integer id) {
        LambdaQueryWrapper<SmsCouponProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCouponProductRelation::getCouponId, Convert.toLong(id));
        return couponProductRelationMapper.selectList(wrapper);
    }

    /**
     * 修改商品分类与优惠券关系
     * @param productRelationList
     */
    @Override
    public void updateIntoProductRelation(List<SmsCouponProductRelation> productRelationList) {
        for (SmsCouponProductRelation relation : productRelationList) {
            couponProductRelationMapper.updateById(relation);
        }
    }
}
